package com.hr.reactive.mono.s02;

import com.hr.reactive.common.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author Administrator
 * 延迟执行
 */
public class Lec06FromCallable {
    private static final Logger log = LoggerFactory.getLogger(Lec06FromCallable.class);

    public static void main(String[] args) {
        var list = List.of(1, 2, 3);
        //如果只是发布者 我们还没有订阅  这时候不需要执行
        Mono.just(sum(list));
       // Mono.just(sum(list)).subscribe(Utils.subscriber());
        //fromCallable 会抛出异常  fromSupplier不会抛出异常
        Mono.fromCallable(()->sum(list)).subscribe(Utils.subscriber());
    }

    private static int sum(List<Integer> list) {
        log.info("finding the sum of {}", list);
        return list.stream().mapToInt(a -> a).sum();
    }
}
